Skip to content

Optimise Iterator::{max, max_by, min, min_by}. #24180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2015

Conversation

huonw
Copy link
Member

@huonw huonw commented Apr 8, 2015

The main change in this patch is removing the use of Option inside the
inner loops of those functions to avoid comparisons where one branch
will only trigger on the first pass through the loop.

The included benchmarks go from:

test bench_max    ... bench:       372 ns/iter (+/- 118)
test bench_max_by ... bench:       428 ns/iter (+/- 33)
test bench_max_by2 ... bench:      7128 ns/iter (+/- 326)

to:

test bench_max    ... bench:       317 ns/iter (+/- 64)
test bench_max_by ... bench:       356 ns/iter (+/- 270)
test bench_max_by2 ... bench:      1387 ns/iter (+/- 183)

Problem noticed in http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/

@rust-highfive
Copy link
Contributor

r? @pcwalton

(rust_highfive has picked a reviewer for you, use r? to override)

@huonw huonw force-pushed the optimise-max-etc branch from 8653363 to b5e62e4 Compare April 8, 2015 04:41
mut f_cmp: FCmp) -> Option<(B, I::Item)>
where I: Iterator,
FProj: FnMut(&I::Item) -> B,
FCmp: FnMut(&B, &I::Item, &B, &I::Item) -> bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reason the comparison function takes 4 arguments, when each client only wants 2 of them, instead of just making it take the projections and having max() / min() use |x| x for the projection function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it would be nice but the lifetimes don't work out, B is disconnected from &I::Item. I think the fact we move sel and x around internally means that this approach can't work. :(

@alexcrichton alexcrichton assigned alexcrichton and unassigned pcwalton Apr 9, 2015
@alexcrichton
Copy link
Member

r=me with nits addressed, nice wins!

The main change in this patch is removing the use of `Option` inside the
inner loops of those functions to avoid comparisons where one branch
will only trigger on the first pass through the loop.

The included benchmarks go from:

    test bench_max    ... bench:       372 ns/iter (+/- 118)
    test bench_max_by ... bench:       428 ns/iter (+/- 33)
    test bench_max_by2 ... bench:      7128 ns/iter (+/- 326)

to:

    test bench_max    ... bench:       317 ns/iter (+/- 64)
    test bench_max_by ... bench:       356 ns/iter (+/- 270)
    test bench_max_by2 ... bench:      1387 ns/iter (+/- 183)

Problem noticed in http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
@huonw huonw force-pushed the optimise-max-etc branch from b5e62e4 to c2258d6 Compare April 10, 2015 04:42
@huonw
Copy link
Member Author

huonw commented Apr 10, 2015

@bors r=alexcrichton

@bors
Copy link
Collaborator

bors commented Apr 10, 2015

📌 Commit c2258d6 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Apr 10, 2015

⌛ Testing commit c2258d6 with merge f4aa02e...

Manishearth added a commit to Manishearth/rust that referenced this pull request Apr 10, 2015
The main change in this patch is removing the use of `Option` inside the
inner loops of those functions to avoid comparisons where one branch
will only trigger on the first pass through the loop.

The included benchmarks go from:

    test bench_max    ... bench:       372 ns/iter (+/- 118)
    test bench_max_by ... bench:       428 ns/iter (+/- 33)
    test bench_max_by2 ... bench:      7128 ns/iter (+/- 326)

to:

    test bench_max    ... bench:       317 ns/iter (+/- 64)
    test bench_max_by ... bench:       356 ns/iter (+/- 270)
    test bench_max_by2 ... bench:      1387 ns/iter (+/- 183)

Problem noticed in http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
@bors
Copy link
Collaborator

bors commented Apr 10, 2015

⛄ The build was interrupted to prioritize another pull request.

@bors
Copy link
Collaborator

bors commented Apr 10, 2015

⌛ Testing commit c2258d6 with merge 1302028...

Manishearth added a commit to Manishearth/rust that referenced this pull request Apr 10, 2015
The main change in this patch is removing the use of `Option` inside the
inner loops of those functions to avoid comparisons where one branch
will only trigger on the first pass through the loop.

The included benchmarks go from:

    test bench_max    ... bench:       372 ns/iter (+/- 118)
    test bench_max_by ... bench:       428 ns/iter (+/- 33)
    test bench_max_by2 ... bench:      7128 ns/iter (+/- 326)

to:

    test bench_max    ... bench:       317 ns/iter (+/- 64)
    test bench_max_by ... bench:       356 ns/iter (+/- 270)
    test bench_max_by2 ... bench:      1387 ns/iter (+/- 183)

Problem noticed in http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
@bors
Copy link
Collaborator

bors commented Apr 10, 2015

⛄ The build was interrupted to prioritize another pull request.

@bors
Copy link
Collaborator

bors commented Apr 10, 2015

⌛ Testing commit c2258d6 with merge e4f9ddb...

bors added a commit that referenced this pull request Apr 10, 2015
The main change in this patch is removing the use of `Option` inside the
inner loops of those functions to avoid comparisons where one branch
will only trigger on the first pass through the loop.

The included benchmarks go from:

    test bench_max    ... bench:       372 ns/iter (+/- 118)
    test bench_max_by ... bench:       428 ns/iter (+/- 33)
    test bench_max_by2 ... bench:      7128 ns/iter (+/- 326)

to:

    test bench_max    ... bench:       317 ns/iter (+/- 64)
    test bench_max_by ... bench:       356 ns/iter (+/- 270)
    test bench_max_by2 ... bench:      1387 ns/iter (+/- 183)

Problem noticed in http://www.reddit.com/r/rust/comments/31syce/using_iterators_to_find_the_index_of_the_min_or/
@bors bors merged commit c2258d6 into rust-lang:master Apr 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants